function curveSum=Plot_MultiPeaksMultiCurves(pars,originals)
% ===============================================
% Schniepp Lab, 2018-2021
% Used for plot multiple curves
% Parameters:
% originals - original data
% dimension of originals (cell type):
% original{m}, original{i}=x*y
% x - x data
% y - y data
% m - number of curves
% dimension of pars:
% (m*2+3)*n
% m - number of curves
% n - number of peaks
% positions - positions for each sub-peak,
%     first line in pars
% width - width for each sub-peak
%     second line in pars
% L - Lorenzian percentage for each sub-peak
%     third line in pars
% Rest of pars:
% curve 1 AG
% curve 1 AL
% curve 2 AG
% curve 2 AL
% ...
% curve n AG
% curve n AL
% ===============================================
    
    curveSum=0;
    [a,n]=size(pars);
    m=(a-3)/2;
    positions=pars(1,:);
    width=pars(2,:);
    L=pars(3,:);
    AGL=pars(4:end,:);
    figure();
    xData=cell(1,m);
    
    for i=1:m
        s=subplot(m,1,i);
        temp=0;
        temp=originals{i};
        xData{i}=linspace(min(temp(:,1)),max(temp(:,1)),500);   %Assign x data to plot subpeaks
        
        plot(temp(:,1),temp(:,2),'LineWidth',1.5);
        hold on;
        grid(s,'on');
        
        tempX=xData{i};
        tempLegend=cell(1,n+2);
        tempLegend{1}='original';
        for j=1:n
            plot(tempX,GaussianLorenzNew(AGL(i*2-1,j),AGL(i*2,j),width(1,j),positions(1,j),L(1,j),tempX),'LineWidth',1.5);
            tempLegend{j+1}=strcat('(',num2str(j),') ',num2str(positions(1,j)));
        end
        
        tempCurve=MultiPeaksMultiCurves(pars,tempX);
        plot(tempX,tempCurve(:,i),'LineWidth',1.5);
        tempLegend{n+2}='fitted';
        
        if n+2>10
            h=legend(tempLegend,'NumColumns',2);
            t=h.Position;
            t(1)=t(1)+4*0.045;
            t(2)=t(2)+3*0.045;
            h.Position=t;
        else
            h=legend(tempLegend);
            t=h.Position;
            t(1)=t(1)+4*0.045;
            t(2)=t(2)+3j*0.045;
            h.Position=t;
        end
    end
    
    set(gcf,'position',[107 284 727 521]);
end